<?php

namespace app\Modules\Api\controllers;

use Yii;
use app\Modules\Api\models\Bill;
use app\Modules\Api\models\User;
use app\filters\Json;
use app\helpers\Request;
use yii\data\Pagination;

/**
 * Class BillController
 * @package app\Modules\Api\controllers
 */
class BillController extends ControllerBase
{
    public function actionIndex()
    {
        $user_id = Request::strictpost('user_id','缺少用户id');


        $user = User::findOne($user_id);
        if(!$user) {
            return Json::error('找不到此用户');
        }

        $query = Bill::find()->where(['user_id'=>$user_id]);
        $type = Yii::$app->request->post('type',0);
        if($type){
            $query->andWhere(['>=','money',0]);
        }else{
            $query->andWhere(['<','money',0]);
        }
        $count = $query->count();
        $pagination = new Pagination([
            'totalCount' => $count,
            'pageSize'=>Yii::$app->request->post('pageSize',6),
            'page'=>Yii::$app->request->post('page'),
        ]);
        $bills = $query
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->orderBy(['billid'=>SORT_DESC])
            ->all();
        return Json::success([
            'user'=>$user->toArray(['take_money','give_money']),
            'bills'=>$bills,
            'bill_count'=>$count,
        ]);
    }
}